﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
* 命名空间：ZTService.Bet
* 文件名：BetAlgorithm
* 创建人：David Rodríguez
* 创建时间：2018/7/31 星期二 9:54:23
* 描述： Pk10下注算法
*************************************************************************/
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using DavidHelper;
using ZTService.Modle;

namespace ZTService.Bet
{
    public class Pk10BetAlgorithm
    {
        private string LastPeriod { get; set; }

        private string LastNumber { get; set; }

        private Timer UserTimer { get; set; } = new Timer();

        public int Uid { get; set; }

        public string Encode { get; set; }

        public List<BetDataModel> BetList { get; set; }=new List<BetDataModel>();

        public bool IsLastSuccess { get; set; } //上次投注是否中

        public int LastBetMoney { get; set; } //上期投注金额

        private static readonly string CheckInUrl = "https://www.yao678.com/API/wis/getBuyNote";

        /// <summary>
        /// 初始化参数，并开始投注
        /// </summary>
        /// <param name="model"></param>
        public Pk10BetAlgorithm()
        {
            StartBet();
        }

        private void StartBet()
        {
            NlogHelper.LogWriter("Pk10智投引擎开启...");
            UserTimer = new Timer
            {
                AutoReset = true,
                Interval = 1000 * 3,
                Enabled = true
            };
            UserTimer.Elapsed += UserTimer_Elapsed;
            UserTimer.Start();
        }

        private void UserTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //获取上期的期数和号码
            var bet = BetHttpData.GetLastKj(KindId.北京赛车);
            if (LastPeriod == bet.Expect) //未开奖
                return;
            LastPeriod = bet.Expect;
            LastNumber = bet.Opencode;
            foreach (var b in BetList)
            {
                SingleBetModle(b);
            }
        }

        private void SingleBetModle(BetDataModel betModel)
        {
            var myLottoryList = new List<BeforeLottory>();
            //按投注位置
            if (betModel.chooseplayname.Equals(BetIdea.位置投注.ToString()))
            {
                myLottoryList = CheckPos(betModel).ToList();
            }
            else if (betModel.chooseplayname.Equals(BetIdea.号码投注.ToString()))
            {
                myLottoryList = CheckNumber(betModel).ToList();
            }

            //按投注模式
            if (betModel.votemode.Equals(Modle.BetModel.常规.ToString()))
            {
                //todo 处理数据
            }
            else if (betModel.votemode.Equals(Modle.BetModel.期期滚.ToString())) //X3
            {
                //todo 处理数据
            }

            //翻倍模式
            if (betModel.doublemodel.Equals(DoubleModel.一直翻倍.ToString()))
            {
                //todo 处理数据
            }
            else if (betModel.doublemodel.Equals(DoubleModel.不中翻倍.ToString()))
            {
                //todo 处理数据
            }
            else if (betModel.doublemodel.Equals(DoubleModel.中翻倍.ToString()))
            {
                //todo 处理数据
            }
            //todo 期期滚？

            //把球号变换成系统对应的球号ID
            var betList = ConvertLottory(myLottoryList).ToList();
            //下注
            CheckIn(betList);
        }


        
        public class Temp
        {
            public int uid { get; set; }
            public string buyinfo { get; set; }
        }
        private void CheckIn(List<Lottory> lottoryList)
        {
            var r = new Temp()
            {
                buyinfo = JsonHelper.SerializeObeject(lottoryList).ToBase64String(),
                uid= Uid
            };
            var json = JsonHelper.SerializeObeject(r);
            var re = HttpHelper.Post(CheckInUrl,json,null, "application/json");
        }

        private IEnumerable<Lottory> ConvertLottory(List<BeforeLottory> beforeList)
        {
            foreach (var b in beforeList)
            {
                var posArr = b.BetPos.Split(',');
                foreach (var p in posArr)
                {
                    yield return new Lottory()
                    {
                        de_id = GetNumId(b.Number, p),
                        valem = b.Money
                    };
                }
            }
        }


        private string GetNumId(string num, string pos)
        {
            var headP = "";
            switch (pos)
            {
                case "1":
                    headP = "冠军";
                    break;
                case "2":
                    headP = "亚军";
                    break;
                case "3":
                    headP = "第三名";
                    break;
                case "4":
                    headP = "第四名";
                    break;
                case "5":
                    headP = "第五名";
                    break;
                case "6":
                    headP = "第六名";
                    break;
                case "7":
                    headP = "第七名";
                    break;
                case "8":
                    headP = "第八名";
                    break;
                case "9":
                    headP = "第九名";
                    break;
                case "10":
                    headP = "第十名";
                    break;
            }

            var tail = num.PadLeft(2,'0');
            var total = $"{headP}_{tail}";
            var bs = Common.SystemList.FirstOrDefault(x=>x.BetEncode== Encode);
            if (bs == null)
            {
                NlogHelper.LogWriter("未找到该用户符合的子系统,请检查encode字段",NlogHelper.LogStatus.Error);
                return "";
            }

            if (bs.Pk10NumberDic.ContainsKey(total))
            {
                return bs.Pk10NumberDic[total];
            }
            else
            {
                NlogHelper.LogWriter("球号系统错误，你整个锤子？",NlogHelper.LogStatus.Error);
                return "";
            }
        }


        /// <summary>
        /// 按位置投注
        /// </summary>
        /// <returns></returns>
        private IEnumerable<BeforeLottory> CheckPos(BetDataModel betmodel)
        {
            var numArr = betmodel.choosenums;
            var lastNumArr = LastNumber.Split(',');
            foreach (var m in numArr)
            {
                //位置投注，找出上期号码 在相应位置的号码   value1:"1,2,3"
                var posArr = m.value1.Split(',');
                for (var i = 0; i < posArr.Length; i++)
                {
                    var p = 0;
                    if (!Utils.IsNumberic(posArr[i], out p) || p > 10)
                        continue;
                    yield return new BeforeLottory()
                    {
                        Number = lastNumArr[p - 1],
                        Money = betmodel.money,
                        BetPos = m.valpos
                    };
                }
            }
        }

        /// <summary>
        /// 按号码投注
        /// </summary>
        /// <returns></returns>
        private IEnumerable<BeforeLottory> CheckNumber(BetDataModel betModel)
        {
            var numArr = betModel.choosenums;
            foreach (var m in numArr)
            {
                //号码投注
                var betNumArr = m.value1.Split(',');
                for (var i = 0; i < betNumArr.Length; i++)
                {
                    var p = 0;
                    if (!Utils.IsNumberic(betNumArr[i], out p) || p > 10)
                        continue;
                    yield return new BeforeLottory()
                    {
                        Number = p.ToString(),
                        Money = betModel.money,
                        BetPos = m.valpos
                    };
                }
            }
        }
    }
}